package com.custom.typeevaluatortext;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import androidx.annotation.Nullable;

/**
 * @description：从A到Z自动播放动画
 * @Author MRyan
 * @Date 2020/3/11 13:45
 * @Version 1.0
 */
public class CustomTextView extends View {
    private int mBgColor;
    private int mCustomSzie;
    private int mTextColor;
    private Paint mBgPaint;
    private Paint mTextPaint;
    private char text = (char) 65;
    private int mCustomTextSize;
    private Rect rect;

    public CustomTextView(Context context) {
        super(context);
    }

    public CustomTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttr(context, attrs);
        initPaint();
        startAni();
    }


    public CustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, 0);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mCustomSzie, mCustomSzie);

    }

    private void initAttr(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
        mBgColor = typedArray.getColor(R.styleable.CustomTextView_CustomeBgColor, mBgColor);
        mCustomSzie = (int) typedArray.getDimension(R.styleable.CustomTextView_CustomSize, mCustomSzie);
        mCustomTextSize = (int) typedArray.getDimension(R.styleable.CustomTextView_CustomTextSize, mCustomTextSize);
        mTextColor = typedArray.getColor(R.styleable.CustomTextView_CustomColor, mTextColor);
        typedArray.recycle();
    }


    private void initPaint() {
        mBgPaint = new Paint();
        mBgPaint.setStyle(Paint.Style.FILL);
        mBgPaint.setColor(mBgColor);
        mBgPaint.setAntiAlias(true);

        mTextPaint = new Paint();
        mTextPaint.setStrokeWidth(25);
        mTextPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mTextPaint.setColor(mTextColor);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(mCustomTextSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int center = mCustomSzie / 2;
        @SuppressLint("DrawAllocation") RectF rectF = new RectF(0, 0, mCustomSzie, mCustomSzie);
        canvas.drawRoundRect(rectF, 70, 70, mBgPaint);
        canvas.drawText(String.valueOf(text), center - 120, center + 120, mTextPaint);
    }

    public void startAni() {
        ValueAnimator animator = ValueAnimator.ofObject(new CharEvaluator(), 'A', 'Z');
        animator.setInterpolator(new AccelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                text = (char) animation.getAnimatedValue();
                postInvalidate();
            }
        });
        animator.setDuration(15000);
        animator.start();
    }


    class CharEvaluator implements TypeEvaluator<Character> {
        @Override
        public Character evaluate(float fraction, Character startValue, Character endValue) {
            int startInt = (int) startValue;
            Log.e("startInt", startInt + "");
            int endInt = (int) endValue;
            Log.e("endInt", endInt + "");
            Log.e("fraction", fraction + "");
            int curInt = (int) (startInt + fraction * (endInt - startInt));
            Log.e("curInt", curInt + "");
            char result = (char) curInt;
            Log.e("result", result + "");
            return result;

        }
    }
}


